{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 10 数据连接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key  data1\n",
      "0   b      2\n",
      "1   b      5\n",
      "2   a      1\n",
      "3   c      4\n",
      "4   a      9\n",
      "5   a      5\n",
      "6   b      2\n",
      "--------------------------------------------------\n",
      "  key  data2\n",
      "0   a      1\n",
      "1   b      6\n",
      "2   d      1\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(12345)\n",
    "df_obj1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],\n",
    "                        'data1' : np.random.randint(0,10,7)})\n",
    "df_obj2 = pd.DataFrame({'key': ['a', 'b' ,'d'],\n",
    "                        'data2' : np.random.randint(0,10,3)})\n",
    "\n",
    "print(df_obj1)\n",
    "print('-'*50)\n",
    "\n",
    "print(df_obj2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-02T17:36:35.113591800Z",
     "start_time": "2024-05-02T17:36:35.102967200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### merge，就是for循环套for循环，交叉相乘\n",
    "### _on = 列名， _index = 行索引\n",
    "### 拿左边的第一个值，去右边遍历寻找相同的值，合成一行。然后拿下一个左边的值重复。最后左值相同的挤一块\n",
    "### 如果用来merge的列中，没有相同的值（比如左边abc，右边def），则返回空"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "outputs": [
    {
     "data": {
      "text/plain": "  key  data1  data2\n0   b      2      6\n1   b      5      6\n2   b      2      6\n3   a      1      1\n4   a      9      1\n5   a      5      1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>data1</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>2</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>5</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>b</td>\n      <td>2</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>a</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>9</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>a</td>\n      <td>5</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 默认连接使用相同的列名，连接方式是内连接\n",
    "pd.merge(df_obj1, df_obj2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-02T16:36:23.441205700Z",
     "start_time": "2024-05-02T16:36:23.401220600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "outputs": [
    {
     "data": {
      "text/plain": "  key_x  data1 key_y  data2\n0     b      2     a      1\n1     b      5     b      6\n2     a      1     d      1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key_x</th>\n      <th>data1</th>\n      <th>key_y</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>2</td>\n      <td>a</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>5</td>\n      <td>b</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>1</td>\n      <td>d</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 拿行索引连接，即相同的行索引才拿来连接\n",
    "pd.merge(df_obj1, df_obj2,left_index=True,right_index=True)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-02T16:27:49.043120Z",
     "start_time": "2024-05-02T16:27:48.915349400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "outputs": [
    {
     "data": {
      "text/plain": "  key  data1  data2\n0   b      2      6\n1   b      5      6\n2   b      2      6\n3   a      1      1\n4   a      9      1\n5   a      5      1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key</th>\n      <th>data1</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>2</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>5</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>b</td>\n      <td>2</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>a</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>9</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>a</td>\n      <td>5</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 左表和右表都拿key列来连接\n",
    "pd.merge(df_obj1, df_obj2, on='key')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-02T16:27:49.043120Z",
     "start_time": "2024-05-02T16:27:48.950035100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "outputs": [],
   "source": [
    "# 更改列名\n",
    "df_obj3 = df_obj1.rename(columns={'key':'key1'})\n",
    "df_obj4 = df_obj2.rename(columns={'key':'key2'})"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-02T16:27:49.043120Z",
     "start_time": "2024-05-02T16:27:48.971503Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "outputs": [
    {
     "data": {
      "text/plain": "  key1  data1\n0    b      2\n1    b      5\n2    a      1\n3    c      4\n4    a      9\n5    a      5\n6    b      2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>data1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>c</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>a</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>b</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_obj3"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-02T16:27:49.043120Z",
     "start_time": "2024-05-02T16:27:48.980333500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "outputs": [
    {
     "data": {
      "text/plain": "  key2  data2\n0    a      1\n1    b      6\n2    d      1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key2</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>a</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>d</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_obj4"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-02T16:27:49.043120Z",
     "start_time": "2024-05-02T16:27:48.993711300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "outputs": [
    {
     "data": {
      "text/plain": "  key1  data1 key2  data2\n0    b      2    b      6\n1    b      5    b      6\n2    b      2    b      6\n3    a      1    a      1\n4    a      9    a      1\n5    a      5    a      1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>data1</th>\n      <th>key2</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>2</td>\n      <td>b</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>5</td>\n      <td>b</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>b</td>\n      <td>2</td>\n      <td>b</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>a</td>\n      <td>1</td>\n      <td>a</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>9</td>\n      <td>a</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>a</td>\n      <td>5</td>\n      <td>a</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 左表以key1来连接，右表以key2来连接\n",
    "# 比pd.merge(df_obj1, df_obj2, on='key')多了一列，因为左右表是key1和key2\n",
    "pd.merge(df_obj3, df_obj4, left_on='key1', right_on='key2')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-02T16:27:49.043120Z",
     "start_time": "2024-05-02T16:27:49.010513600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "outputs": [
    {
     "data": {
      "text/plain": "  key1  data1 key2  data2\n0    b    2.0    b    6.0\n1    b    5.0    b    6.0\n2    b    2.0    b    6.0\n3    a    1.0    a    1.0\n4    a    9.0    a    1.0\n5    a    5.0    a    1.0\n6    c    4.0  NaN    NaN\n7  NaN    NaN    d    1.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>data1</th>\n      <th>key2</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>2.0</td>\n      <td>b</td>\n      <td>6.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>5.0</td>\n      <td>b</td>\n      <td>6.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>b</td>\n      <td>2.0</td>\n      <td>b</td>\n      <td>6.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>a</td>\n      <td>1.0</td>\n      <td>a</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>9.0</td>\n      <td>a</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>a</td>\n      <td>5.0</td>\n      <td>a</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>c</td>\n      <td>4.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>d</td>\n      <td>1.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 全外连接。先merge，再去处理不共有的\n",
    "# 比较key1和key2，如果左表有右表没有，或者右表有左表没有，则用NaN填充\n",
    "pd.merge(df_obj3, df_obj4, left_on='key1', right_on='key2', how='outer')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-02T16:46:49.115534900Z",
     "start_time": "2024-05-02T16:46:49.046837200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "outputs": [
    {
     "data": {
      "text/plain": "  key1  data1 key2  data2\n0    b      2    b    6.0\n1    b      5    b    6.0\n2    a      1    a    1.0\n3    c      4  NaN    NaN\n4    a      9    a    1.0\n5    a      5    a    1.0\n6    b      2    b    6.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>data1</th>\n      <th>key2</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>b</td>\n      <td>2</td>\n      <td>b</td>\n      <td>6.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>b</td>\n      <td>5</td>\n      <td>b</td>\n      <td>6.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>1</td>\n      <td>a</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>c</td>\n      <td>4</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>a</td>\n      <td>9</td>\n      <td>a</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>a</td>\n      <td>5</td>\n      <td>a</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>b</td>\n      <td>2</td>\n      <td>b</td>\n      <td>6.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# left join\n",
    "pd.merge(df_obj3, df_obj4, left_on='key1', right_on='key2', how='left')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-02T16:52:24.734369800Z",
     "start_time": "2024-05-02T16:52:24.721381600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "outputs": [
    {
     "data": {
      "text/plain": "  key1  data1 key2  data2\n0    a    1.0    a      1\n1    a    9.0    a      1\n2    a    5.0    a      1\n3    b    2.0    b      6\n4    b    5.0    b      6\n5    b    2.0    b      6\n6  NaN    NaN    d      1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>key1</th>\n      <th>data1</th>\n      <th>key2</th>\n      <th>data2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>a</td>\n      <td>1.0</td>\n      <td>a</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>a</td>\n      <td>9.0</td>\n      <td>a</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>a</td>\n      <td>5.0</td>\n      <td>a</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>b</td>\n      <td>2.0</td>\n      <td>b</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>b</td>\n      <td>5.0</td>\n      <td>b</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>b</td>\n      <td>2.0</td>\n      <td>b</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>d</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# right join\n",
    "pd.merge(df_obj3, df_obj4, left_on='key1', right_on='key2', how='right')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-02T16:52:26.603355100Z",
     "start_time": "2024-05-02T16:52:26.553129900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 处理重复列名"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key  data_x  data_y\n",
      "0   b       2       6\n",
      "1   b       5       6\n",
      "2   b       2       6\n",
      "3   a       1       1\n",
      "4   a       9       1\n",
      "5   a       5       1\n",
      "--------------------------------------------------\n",
      "  key  data_left  data_right\n",
      "0   b          2           6\n",
      "1   b          5           6\n",
      "2   b          2           6\n",
      "3   a          1           1\n",
      "4   a          9           1\n",
      "5   a          5           1\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(12345)\n",
    "df_obj1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],\n",
    "                        'data' : np.random.randint(0,10,7)})\n",
    "df_obj2 = pd.DataFrame({'key': ['a', 'b', 'd'],\n",
    "                        'data' : np.random.randint(0,10,3)})\n",
    "\n",
    "# 相同的数据列会自动添加后缀\n",
    "print(pd.merge(df_obj1, df_obj2, on='key'))\n",
    "print('-'*50)\n",
    "\n",
    "#给相同的数据列添加后缀\n",
    "print(pd.merge(df_obj1, df_obj2, on='key', suffixes=('_left', '_right')))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-02T17:05:31.192365100Z",
     "start_time": "2024-05-02T17:05:31.157337Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 按索引连接"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key  data1\n",
      "0   b      2\n",
      "1   b      5\n",
      "2   a      1\n",
      "3   c      4\n",
      "4   a      9\n",
      "5   a      5\n",
      "6   b      2\n",
      "--------------------------------------------------\n",
      "   data2\n",
      "a      1\n",
      "b      6\n",
      "d      1\n"
     ]
    }
   ],
   "source": [
    "np.random.seed(12345)\n",
    "df_obj1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],\n",
    "                        'data1' : np.random.randint(0,10,7)})\n",
    "df_obj2 = pd.DataFrame({'data2' : np.random.randint(0,10,3)}, index=['a', 'b', 'd'])\n",
    "\n",
    "print(df_obj1)\n",
    "print('-'*50)\n",
    "\n",
    "print(df_obj2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-02T17:26:04.609867600Z",
     "start_time": "2024-05-02T17:26:04.592876600Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 合并后的 DataFrame 的行索引并不是单纯来自左侧或右侧的索引，而是基于连接操作生成的新索引"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key  data1  data2\n",
      "0   b      2      6\n",
      "1   b      5      6\n",
      "6   b      2      6\n",
      "2   a      1      1\n",
      "4   a      9      1\n",
      "5   a      5      1\n"
     ]
    }
   ],
   "source": [
    "# 只有当 df_obj1 的 'key' 列中的值与 df_obj2 的索引相匹配时，相应的行才会被合并\n",
    "print(pd.merge(df_obj1, df_obj2, left_on='key', right_index=True))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-02T17:34:02.271722500Z",
     "start_time": "2024-05-02T17:34:02.215469900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "outputs": [
    {
     "data": {
      "text/plain": "   data2 key  data1\n2      1   a      1\n4      1   a      9\n5      1   a      5\n0      6   b      2\n1      6   b      5\n6      6   b      2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>data2</th>\n      <th>key</th>\n      <th>data1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>a</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>a</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>1</td>\n      <td>a</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>0</th>\n      <td>6</td>\n      <td>b</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>6</td>\n      <td>b</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>6</td>\n      <td>b</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 只有当 df_obj2 的索引中的值与 df_obj1 的 'key' 列相匹配时，相应的行才会出现在合并结果中\n",
    "pd.merge(df_obj2,df_obj1, left_index=True, right_on='key')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-02T17:29:47.383108400Z",
     "start_time": "2024-05-02T17:29:47.340061100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
